package jdbcinterface;

import java.sql.*;
import java.rmi.*;
import java.util.Properties;

import rmi.DriverAdapter;
import rmi.RemoteConnection;
import rmi.RemoteDriver;


public class GoggleDriver extends DriverAdapter {
	static {
		try {
			new GoggleDriver() ;
		}catch ( Exception e) { }
	}
	
	public GoggleDriver () throws SQLException {
		DriverManager.registerDriver( this ) ; 
	}
	
	public Connection connect(String url, Properties prop) throws SQLException {
		try {
			String newurl = url.replace("jdbc:goggle", "rmi") ;
			int pos = newurl.indexOf( "://") ; 
			if ( pos == -1 ) return null ; 
			pos = newurl.indexOf( "/", pos + 3 ) ; 
			if ( pos == -1 || pos == url.length() - 1 ) return null ; 
			String dbname = newurl.substring( pos + 1 ); 
			newurl = newurl.substring( 0 , pos ) + "/goggle";
			
			RemoteDriver rdvr = (RemoteDriver) Naming.lookup(newurl);
			RemoteConnection rconn = rdvr.connect( dbname );
			return new GoggleConnection(rconn);
		}
		catch (Exception e) {
			e.printStackTrace() ; 
			throw new SQLException(e.toString() );
		}
	}

	public boolean acceptsURL(String url) throws SQLException {
		if ( url.startsWith( "jdbc:goggle://")) return true; 
		else return false; 
	}
	
}
